<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入姓名"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="手机号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input v-model="model.phone" placeholder="请输入手机号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="最新进展" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea autosize v-model="model.remark" placeholder="请输入最新进展"></a-textarea>
            </a-form-model-item>
          </a-col>
          <a-col :span="10">
            <a-form-model-item label="省份" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="province">
              <a-select type="list_multi" v-model="model.province" placeholder="请选择省份" @change="getCity(model.province)" :disabled="disabled">
                <a-select-option v-for="(i,index) in provinceList" :value="i.sid" :key="index">
                  {{i.name}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="7">
            <a-form-model-item label="城市" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="city">
              <a-select type="list_multi" v-model="model.city" placeholder="请选择城市" @change="getDistrict(model.city)" :disabled="disabled">
                <a-select-option v-for="(i,s) in cityList" :value="i.sid" :key="s">
                  {{i.name}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="7">
            <a-form-model-item label="区/县" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="area">
              <a-select type="list_multi" v-model="model.area" placeholder="请选择区/县" :disabled="disabled">
                <a-select-option v-for="(i,b) in districtList" :value="i.sid" :key="b">
                  {{i.name}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-checkbox-group v-model="model.checkIds" name="checkboxgroup" @change="onChange" style="padding: 0 12%">
            <a-col :span="24" v-for="(option, key) in formCustomerOption" style="padding: 5px 0" :key="key">
              <a-checkbox :key="key" :value="option.value">{{ option.title }}</a-checkbox>
            </a-col>
          </a-checkbox-group>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'CustomerForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    },
    customerOption: {
      type: Array,
      default: [],
      required: true
    },
    provinceList: {
      type: Array,
      default: [],
      required: true
    }

  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {},
      cityList: [],
      districtList: [],
      url: {
        add: '/customer/add',
        edit: '/customer/edit',
        queryById: '/customer/queryById',
        getCity: '/shop/shopBasic/selectAreaCityList',
        getDistrict: '/shop/shopBasic/selectAreaDistrictList',
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
    formCustomerOption() {
      return this.customerOption
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    onChange(checkedValues) {
      console.log('checked = ', checkedValues)
      console.log('value = ', this.value)
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
      if (record.province) {
        this.getCity(record.province)
      }
      if (record.city) {
        this.getDistrict(record.city)
      }
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },// 选择省份
    getCity(e) {
      if (e === null) return
      this.cityList = []
      getAction(this.url.getCity + '/' + e).then(res => {
        if (res.success) {
          this.cityList = res.result
        }
      })
    },
    // 选择城市
    getDistrict(e) {
      if (e === null) return
      this.districtList = []
      getAction(this.url.getDistrict + '/' + e).then(res => {
        if (res.success) {
          this.districtList = res.result
        }
      })
    },
  }
}
</script>